<template>
    <van-form>
      <van-field
        v-model="user.login_id" label="手机号码" 
        :rules="[{ required: true, message: '请填写电话' }]"/>
      <van-field v-model="user.login_pwd" type="password" label="密码" />
      <van-field v-model="passwd" type="password" label="确认密码" v-show="showRegist"/>  
      <van-field
        v-model="user.name"  label="称呼"
        :rules="[{ required: true, message: '请填写称呼' }]" v-show="showRegist"/>
      <van-field
        v-model="user.job" label="职业" 
        :rules="[{ required: true, message: '请填写职业' }]" v-show="showRegist"/>
      <van-field
        v-model="user.memo"  label="其他说明" 
        :rules="[{ required: false, message: '请填写其他说明' }]" v-show="showRegist"/>
        
      <van-field name="checkbox" label="------">
        <template #input>
          <van-button size="small" type="info" @click="onSubmit">提交</van-button>
          <van-checkbox v-model="showRegist">注册</van-checkbox>
        </template>
      </van-field>
      
    </van-form>
</template>

<script>
import { login,insert,regist } from "@/api/common";
import { Toast } from 'vant';

export default {
    data() {
      return {
        showRegist: false,
        user: {
          login_id:'',login_pwd:'',name:'',memo:'',job:''
          },
        passwd: '',
      }
    },
    mounted() {
    },
    methods: {
        onSubmit() {
          if(this.showRegist)
            this.onRegist();
          else
            this.onLogin();
        },
        onLogin() {
            let params = {
                'loginId': this.user.login_id,
                'loginPwd': this.user.login_pwd
            };
            login(params)
            .then( res=> {
                var data = res.data;
                if(data.userId!='') {
                  this.$store.state.loginId = this.user.login_id;
                  this.$store.state.userId = data.userId;
                  this.$store.state.userRole = data.userRole;
                  this.$store.state.userRoleId = data.userRoleId;
                  console.info(data);
                  this.$emit('onLogin',this.userId);
                }
                else {
                  this.getUserInfo();
                }
            })
            .catch(function (error){
                console.log('登录失败,服务器错误');
            });
        },
        getUserInfo() {
            getLoginUser()
            .then( res=> {
              var data = res.data;
              if(data.userId!='') {
                  this.$store.state.loginId = this.user.login_id;
                  this.$store.state.userRole = data.userRole;
                  this.$store.state.userId = data.userId;
                  this.$store.state.userRoleId = data.userRoleId;
                  console.info(data);
                  this.$emit('onLogin',this.userId);
              }
              else {
                Toast.fail('获得浏览权限失败:');
              }
            })
            .catch(function (error){
                this.$message('操作失败...');
            });
        },
        onRegist() {
            let params = {
                'loginId': this.user.login_id,
                'loginPwd': this.user.login_pwd,
                'name': this.user.name,
                'memo': this.user.memo,
                'job': this.user.job
            };
            regist(params)
            .then( res=> {
                var data = res.data;
                if(data.msg=='success') {
                  this.showRegist = false;
                  Toast.fail('注册成功，请登录');
                }
                else {
                  Toast.fail('注册失败:'+data.msg);
                }
            })
            .catch(function (error){
                console.log('注册失败,服务器错误');
            });            
        },
    }
}
</script>